<?php
/**
 * Created by JetBrains PhpStorm.
 * User: QuangRau
 * Date: 9/10/12
 * Time: 7:45 PM
 * To change this template use File | Settings | File Templates.
 */
class Plun_Business_User
{
    public static function insertUserInfo($params)
    {
        try {
            //Get DB instance
            $db = Plun_Db::getInstance('master');

            //Query
            $db->insert("user", $params);

            //Get result
            $id = $db->lastInsertId();

            //Return
            return $id;
        } catch (Exception $e) {
            throw new Plun_Exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * Get list country
     * @static
     * @return mixed
     */
    public static function getListCountry()
    {
        try {
            //Get DB instance
            $db = Plun_Db::getInstance('slave');

            //Query
            $stmt = $db->query("SELECT * FROM country");

            //Get result
            $result = $stmt->fetchAll();

            //Return
            return $result;
        } catch (Exception $e) {
            throw new Plun_Exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * Get list location
     * @static
     * @return mixed
     */
    public static function getListLocation()
    {
        try {
            //Get DB instance
            $db = Plun_Db::getInstance('slave');

            //Query
            $stmt = $db->query("SELECT * FROM location");

            //Get result
            $result = $stmt->fetchAll();

            //Return
            return $result;
        } catch (Exception $e) {
            throw new Plun_Exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * Get list districts
     * @static
     * @return mixed
     */
    public static function getListDistrict()
    {
        try {
            //Get DB instance
            $db = Plun_Db::getInstance('slave');

            //Query
            $stmt = $db->query("SELECT * FROM districts");

            //Get result
            $result = $stmt->fetchAll();

            //Return
            return $result;
        } catch (Exception $e) {
            throw new Plun_Exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * Get user info
     * @static
     * @param $intUserId
     * @return mixed
     * @throws Plun_Exception
     */
    public static function getUserInfo($intUserID)
    {
        try {
            //Get DB instance
            $db = Plun_Db::getInstance('slave');

            //Query
            $stmt = $db->query("SELECT * FROM user WHERE user_id = '" . $intUserID . "'");

            //Get data
            $result = $stmt->fetch();

            //return data
            return $result;
        } catch (Exception $e) {
            throw new Plun_Exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * Check user login
     * @static
     * @param $params
     * @throws Plun_Exception
     */
    public static function checkUserLogin($params)
    {
        try {
            //Get DB instance
            $db = Plun_Db::getInstance('slave');

            //Query
            $stmt = $db->query("SELECT user_id, user_pass FROM user WHERE user_name = '" . $params['user_name'] . "'");

            //Get password
            $result = $stmt->fetch();

            //Check password
            if($result['user_pass'] == md5($params['user_pass'])){
                return $result['user_id'];
            }

            //Return result
            return 0;

        } catch (Exception $e) {
            throw new Plun_Exception($e->getMessage(), $e->getCode());
        }
    }
}
